#include <BulletClassDataSource/PhysicsWorldBullet.h>
#include <osg/Node>
#include <iostream>
#include <EDesignerCore.h>
#include <QDateTime>

#include <EngineFunctions.h>
#include <PhysicsWorldFunctions.h>
#include <PhysicsWorld.h>
#include <NodeInfoContainerBullet.h>
#include <SDFManager.h>
PhysicsWorldBullet::PhysicsWorldBullet( QObject *parent /*= 0*/ ) : ObjectBullet(parent)
{
    refreshItemTest();
    refreshInfoContainer();
    PhysicsWorld::instance()->setUserPointer(this->getInfoContainer());
}

PhysicsWorldBullet::~PhysicsWorldBullet()
{
    PhysicsWorld::instance()->setUserPointer(NULL);
}

void PhysicsWorldBullet::setDebugDraw(bool boolValue)
{
    if(boolValue == true)
    {
        PhysicsWorldFunctions::enablePhysicsDebugDraw();
    }
    else
    {
        PhysicsWorldFunctions::disablePhysicsDebugDraw();
    }
}

bool PhysicsWorldBullet::getDebugDraw()
{
    return PhysicsWorldFunctions::getEnableDebugDraw();
}

void PhysicsWorldBullet::setGravity(const QVector3D &gravity)
{
    PhysicsWorld::instance()->getDynamicsWorld()->setGravity(btVector3( gravity.x(), gravity.y(), gravity.z() ));

    //std::cout<<"PhysicsWorldBullet::setGravity ="<<gravity.x()<<","<<gravity.y()<<","<<gravity.z()<<std::endl;
}

QVector3D PhysicsWorldBullet::getGravity()
{
    double x = PhysicsWorld::instance()->getDynamicsWorld()->getGravity()[0];
    double y = PhysicsWorld::instance()->getDynamicsWorld()->getGravity()[1];
    double z = PhysicsWorld::instance()->getDynamicsWorld()->getGravity()[2];


    QVector3D g;
    g.setX(x);
    g.setY(y);
    g.setZ(z);

    //std::cout<<"PhysicsWorldBullet::getGravity ="<<g.x()<<","<<g.y()<<","<<g.z()<<std::endl;

    return g;
}

QVarintTrigger PhysicsWorldBullet::getTriggerCollisionOnce()
{
    return QVarintTrigger();
}

void PhysicsWorldBullet::setTriggerCollisionOnce(QVarintTrigger trigger)
{
    PhysicsWorld::instance()->performCollisionOnce();
}

QVarintTrigger PhysicsWorldBullet::getTriggerSaveRigidInfo()
{
    return QVarintTrigger();
}

void PhysicsWorldBullet::setTriggerSaveRigidInfo(QVarintTrigger trigger)
{
    //std::cout<<"PhysicsWorldBullet::setTriggerSaveRigidInfo"<<std::endl;

    PhysicsWorldFunctions::writeRigidBodyInfoToXML();
    PhysicsWorldFunctions::writeZipOfRigidBody();
}

QVarintTrigger PhysicsWorldBullet::getTriggerReadSDFInfo()
{
    return QVarintTrigger();
}

void PhysicsWorldBullet::setTriggerReadSDFInfo(QVarintTrigger trigger)
{
    //std::cout<<"PhysicsWorldBullet::setTriggerReadSDFInfo"<<std::endl;
    SDFManager::instance()->readFromFile("tempFiles/SDFInfo/sdfInfo.xml");
}

QVarintTrigger PhysicsWorldBullet::getTriggerComputeDof()
{
    return QVarintTrigger();
}

void PhysicsWorldBullet::setTriggerComputeDof(QVarintTrigger trigger)
{
    SDFManager::instance()->computeSDFDofAll();
}

QVarintTrigger PhysicsWorldBullet::getTriggerDisassemblySequence()
{
    return QVarintTrigger();
}

void PhysicsWorldBullet::setTriggerDisassemblySequence(QVarintTrigger trigger)
{
    SDFManager::instance()->computeDisassemblySequenceAll();
}




